package cn.tedu.ymg.product.service.impl;

import cn.tedu.ymg.commons.ex.ServiceException;
import cn.tedu.ymg.commons.pojo.vo.PageData;
import cn.tedu.ymg.commons.util.PageInfoToPageDataConverter;
import cn.tedu.ymg.commons.web.ServiceCode;
import cn.tedu.ymg.product.mapper.HotelMapper;
import cn.tedu.ymg.product.pojo.entity.Hotel;
import cn.tedu.ymg.product.pojo.param.HotelAddNewParam;
import cn.tedu.ymg.product.pojo.param.HotelUpdateInfoParam;
import cn.tedu.ymg.product.pojo.vo.HotelListItemVO;
import cn.tedu.ymg.product.pojo.vo.HotelStandardVO;
import cn.tedu.ymg.product.pojo.vo.HotelXQVO;
import cn.tedu.ymg.product.service.IHotelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class HotelServiceImpl implements IHotelService {

    @Autowired
    HotelMapper HotelMapper;



    @Override
    public void addNew(HotelAddNewParam hotelAddNewParam) {

        log.debug("开始处理【添加酒店的业务】，参数", hotelAddNewParam);

        //将酒店写入到数据库中
        Hotel Hotel = new Hotel();
        BeanUtils.copyProperties(hotelAddNewParam, Hotel);

        int rows = HotelMapper.insert(Hotel);
        if (rows !=1){
            String Hotel1 = "添加酒店失败，服务器忙，请稍后再试！";
            log.warn(Hotel1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Hotel1);
        }
        log.debug("将新的酒店数据写入到数据库，完成！");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除酒店】的业务，参数：{}", id);
        Hotel Hotel = HotelMapper.selectById(id);
        if (Hotel ==null){
            String Hotel1 = "删除酒店失败，Id不存在";
            log.warn(Hotel1);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Hotel1);
        }

        int rows = HotelMapper.deleteById(id);
        if (rows!=1){
            String Hotel1 = "删除酒店失败，服务器忙，请稍后再试！";
            log.warn(Hotel1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Hotel1);
        }
        log.debug("从数据库删除酒店，完成！");
    }

    @Override
    public void updateInfoById(Long id, HotelUpdateInfoParam hotelUpdateInfoParam) {

        log.debug("开始处理【修改酒店】的业务，ID:{},新数据：{}", id, hotelUpdateInfoParam);

        //检查酒店是否存在，如果不存在，则抛出异常
        QueryWrapper<Hotel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = HotelMapper.selectCount(queryWrapper);
        log.debug("根据示例ID统计匹配的酒店数量，结果：{}", countById);
        if (countById == 0) {
            String Hotel = "修改酒店失败，酒店数据不存在！";
            log.warn(Hotel);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Hotel);
        }

        //执行修改
        Hotel Hotel = new Hotel();
        BeanUtils.copyProperties(hotelUpdateInfoParam, Hotel);
        Hotel.setId(id);
        int rows = HotelMapper.updateById(Hotel);
        if (rows!=1){
            String Hotel1 = "修改酒店失败，服务器忙，请稍后再试！";
            log.warn(Hotel1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Hotel1);
        }
        log.debug("从数据库修改酒店，完成！");
    }

    @Override
    public HotelStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询酒店】的业务，参数：{}", id);
        HotelStandardVO queryResult = HotelMapper.getStandardById(id);
        if (queryResult==null){
            String Hotel = "查询酒店失败，酒店数据不存在！";
            log.warn(Hotel);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,Hotel);
        }
        queryResult.setXq(HotelMapper.getUrlById(id));
        return queryResult;
    }
    @Override

    public PageData<HotelListItemVO> list(Integer pageNum) {
        Integer pageSize = 6;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<HotelListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询酒店列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<HotelListItemVO> list =  HotelMapper.list();
        PageInfo<HotelListItemVO> pageInfo = new PageInfo<>(list);
        PageData<HotelListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<HotelXQVO> getUrlById(Long id) {
        log.debug("开始处理【根据ID查询酒店详情图片】的业务，参数：{}", id);
        QueryWrapper<Hotel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = HotelMapper.selectCount(queryWrapper);
        log.debug("根据示例ID统计匹配的方案数量，结果：{}", countById);
        if (countById == 0) {
            String Case = "查询酒店详情图片失败，酒店id不存在！";
            log.warn(Case);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Case);
        }

        List<HotelXQVO> list = HotelMapper.getUrlById(id);
        PageInfo<HotelXQVO> pageInfo = new PageInfo<>(list);
        PageData<HotelXQVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
